/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DTO.user.ObjectFactory;
import DTO.user.Users;
import DTO.user.Users.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author TungNT60513
 */
public class UserDAO {
    // TungNT

    public boolean add(User user) {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = DBConnection.getConnection();
            stm = con.prepareStatement("INSERT INTO [Users](Username,Password,Email,isAdmin) VALUES(?,?,?,?)");
            stm.setString(1, user.getUsername());
            stm.setString(2, user.getPassword());
            stm.setString(3, user.getEmail());
            stm.setBoolean(4, user.isIsAdmin());

            stm.executeUpdate();
            return true;
        } catch (SQLException ex) {
            System.out.print("SQL Exception Error: " + ex.getMessage());
            return false;
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    System.out.println("Connection error: " + ex.getMessage());
                }
            } else if (stm != null) {
                try {
                    stm.close();
                } catch (SQLException ex) {
                    System.out.println("SQL Statement error " + ex.getMessage());
                }
            } // end else if
        } // end finally
    } // public boolean add(Users user)

    // TungNT
    public boolean delete(String username) {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = DBConnection.getConnection();
            stm = con.prepareStatement("DELETE FROM [USERS] WHERE USERNAME=?");
            stm.setString(1, username);
            stm.executeUpdate();
            return true;
        } catch (SQLException ex) {
            System.out.print("SQL Exception Message:" + ex.getMessage());
            return false;
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    System.out.println("Connection error: " + ex.getMessage());
                }
            } else if (stm != null) {
                try {
                    stm.close();
                } catch (SQLException ex) {
                    System.out.println("SQL Statement error " + ex.getMessage());
                }
            } // end else if
        } // end finally
    } // end public boolean delete(String username)

    // TungNT
    public boolean update(User user) {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = DBConnection.getConnection();
            stm = con.prepareStatement("UPDATE [Users] SET Password=?,Email=?,isAdmin=?"
                    + " WHERE Username=?");
            stm.setString(1, user.getPassword());
            stm.setString(2, user.getEmail());
            stm.setBoolean(3, user.isIsAdmin());
            stm.executeUpdate();

            return true;
        } catch (SQLException ex) {
            System.out.print("SQL Exception Error: " + ex.getMessage());
            return false;
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    System.out.println("Connection error: " + ex.getMessage());
                }
            } else if (stm != null) {
                try {
                    stm.close();
                } catch (SQLException ex) {
                    System.out.println("SQL Statement error " + ex.getMessage());
                }
            } // end else if
        } // end finally
    } // end public boolean update(Users user)

    // TungNT
    public Users selectAllUsers() {
        Connection con = null;
        PreparedStatement stm = null;
        Users users = null;
        ObjectFactory objf = new ObjectFactory();
        try {
            con = DBConnection.getConnection();
            stm = con.prepareStatement("SELECT * FROM [USERS]");
            ResultSet rs = stm.executeQuery();
            users =  new Users();
            while (rs.next()) {
                User user = new User();

                user.setUsername(rs.getString("Username"));
                user.setEmail(rs.getString("Email"));
                user.setPassword(rs.getString("Password"));
                user.setIsAdmin(rs.getBoolean("isAdmin"));

                users.getUser().add(user);
                
            }
            return users;
        } catch (SQLException ex) {
            System.out.println("SQL Exception ex: " + ex.getMessage());
            return null;
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    System.out.println("SQL Exception ex: " + ex.getMessage());
                }
            } // end if (con != null)
            if (stm != null) {
                try {
                    stm.close();
                } catch (SQLException ex) {
                    System.out.println("SQL Exception ex: " + ex.getMessage());
                }
            } // end if (stm != null)
        } // end finally
    } // end public Users selectAllUsers(User user)
} // end public class UserDAO

